package linter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linter.POJO.DO.*;
import linter.POJO.DTO.CollectDTO;
import linter.mapper.*;
import linter.service.CollectService;
import linter.utils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static linter.custom.Constant.DEFAULT_IMG_URL_NOTE;
import static linter.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【collect(收藏组详情表)】的数据库操作Service实现
* @createDate 2024-03-15 11:14:15
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService{

    @Resource
    CollectMapper collectMapper;
    @Resource
    PostsMapper postsMapper;
    @Resource
    NoteMapper noteMapper;
    @Resource
    PostUserRelationsMapper postUserRelationsMapper;
    @Resource
    CollectPostsRelationMapper collectPostsRelationMapper;
    @Resource
    CollectNoteRelationMapper collectNoteRelationMapper;


    @Override
    public String create(String collectName, Long uuid) {
        Date date = new Date();
        Collect collect = new Collect();
        collect.setCollectImage(DEFAULT_IMG_URL_NOTE);
        collect.setCollectName(collectName);
        collect.setUuid(uuid);
        collect.setCreateTime(date);
        collect.setUpdateTime(date);
        collect.setCollectFatherId(0);
        collect.setCollectSonId(0);
        int flag = collectMapper.insert(PropertyUtils.doToDto(collect, Collect.class));
        return flag != 1 ? CREATION_ERROR : CREATION_SUCCESS;
    }

    @Override
    public String createByFather(String collectName, Long uuid, int collectFatherId) {
        Date date = new Date();
        Collect collect = new Collect();
        collect.setCollectImage(DEFAULT_IMG_URL_NOTE);
        collect.setCollectName(collectName);
        collect.setUuid(uuid);
        collect.setCreateTime(date);
        collect.setUpdateTime(date);
        collect.setCollectFatherId(collectFatherId);
        collect.setCollectSonId(0);
        int flag = collectMapper.insert(PropertyUtils.doToDto(collect, Collect.class));
        if (collectMapper.updateByFatherId(collectFatherId, collect.getId())){ return flag != 1 ? CREATION_ERROR : CREATION_SUCCESS; }
        return "父收藏夹不存在";
    }

    @Override
    public String del(Integer collectId) {
        return collectMapper.deleteByCollectID(collectId) == 1 ? DELETE_SUCCESS : DELETE_ERROR;
    }

    @Override
    public String collectPosts(Long uuid, Integer postsId, Long collectId) {
        PostUserRelations postUserRelations = new PostUserRelations();
        postUserRelations.setPostsId(postsId);
        postUserRelations.setUuid(uuid);
        int flag = postUserRelationsMapper.insert(postUserRelations);
        CollectPostsRelation collectPostsRelation = new CollectPostsRelation();
        collectPostsRelation.setCollectId(collectId);
        collectPostsRelation.setPostsId(postsId);
        flag = flag + collectPostsRelationMapper.insert(collectPostsRelation);
        if (flag == 2){
            postsMapper.updateByCollectNub(postsId);
            return "收藏成功";
        }
        return "收藏失败";
    }

    @Override
    public PageInfo<CollectDTO> selAllByUuid(Integer pageNum, Integer pageSize, Long uuid) {
        PageHelper.startPage(1, 10);
        List<CollectDTO> list = collectMapper.selAllByUuid(uuid);
        return new PageInfo<>(list);
    }

    @Override
    public String delCollectPosts(Long uuid, Integer collectId, Integer postsId) {
        int flag = postUserRelationsMapper.delete(new QueryWrapper<PostUserRelations>()
                .eq("uuid", uuid).eq("posts_id", postsId));
        flag = flag + collectPostsRelationMapper.delete(new QueryWrapper<CollectPostsRelation>()
                .eq("collect_id", collectId).eq("posts_id", postsId));
        flag = flag+postsMapper.updateByCollectNubDel(postsId);
        return flag == 3 ? DELETE_SUCCESS : DELETE_ERROR;
    }

    @Override
    public String updateCollect(Integer collectId, String collectName, Long uuid) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        Collect collect = new Collect();
        collect.setCollectName(collectName);
        int flag = collectMapper.update(collect, queryWrapper.eq("id", collectId).eq("uuid", uuid));
        return  flag == 1 ? UPDATE_SUCCESS : UPDATE_ERROR;
    }

    @Override
    public String moveCollect(Integer collectId, Integer collectFatherId, Long uuid) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        Collect collect = new Collect();
        collect.setCollectFatherId(collectFatherId);
        int flag = collectMapper.update(collect, queryWrapper.eq("id", collectId).eq("uuid", uuid));
        if (flag != 1){ return "移动失败"; }
        return "移动成功";
    }

    @Override
    public PageInfo<?> selCollectPosts(Long uuid, Integer collectId) {
        PageHelper.startPage(1,10);
        QueryWrapper<CollectPostsRelation> qwCPR = new QueryWrapper<>();
        List<CollectPostsRelation>  collectPostsRelations = collectPostsRelationMapper.selectList(qwCPR
                .eq("collect_id", collectId));

        List<Integer> postIds = collectPostsRelations.stream()
                .filter(Objects::nonNull)
                .map(CollectPostsRelation::getPostsId)
                .collect(Collectors.toList());
        List<Posts> posts = new ArrayList<>();
        for (Integer postId : postIds) {
            Posts post = postsMapper.selectById(postId);
            if (post != null) { posts.add(post); }
        }
        return new PageInfo<>(posts);
    }

    @Override
    public PageInfo<?> selCollectNote(Long uuid, Integer collectId) {
        PageHelper.startPage(1, 10);
        QueryWrapper<CollectNoteRelation> qwCNR = new QueryWrapper<>();
        List<CollectNoteRelation> collectNoteRelations = collectNoteRelationMapper.selectList(qwCNR
                .eq("collect_id", collectId));
        List<Long> noteIds = collectNoteRelations.stream()
                .filter(Objects::nonNull)
                .map(CollectNoteRelation::getNoteId)
                .collect(Collectors.toList());
        List<Note> notes = new ArrayList<>();
        for (Long noteId : noteIds) {
            Note note = noteMapper.selectById(noteId);
            if (note != null) {
                notes.add(note);
            }
        }
        return new PageInfo<>(PropertyUtils.listToList(notes, Note.class));
    }
}




